﻿using DayuCode.Designer.Entity;
using DayuCode.Designer.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DayuCode.Designer.Service
{
    public class DataTableHandler
    {
        private static string getBaseUrl(string methodName)
        {
            return AppInstance.ApiServer + "api/base/dayucode_dataTableHandler/" + methodName;
        }

		public static HDataTable getDataTableWithColumns(long id)
		{
			HDataTable obj = QHttpClient.Post<HDataTable>(getBaseUrl("getDataTableWithColumns"), new { id = id });
			return obj;
		}

		public static HDataTable saveDataTable(HDataTable item)
		{
			HDataTable obj = QHttpClient.Post<HDataTable>(getBaseUrl("saveDataTable"), new { item = item });
			return obj;
		}

		/// <summary>
		/// 获取功能涉及的所有的数据表
		/// </summary>
		/// <param name="projectId"></param>
		/// <returns></returns>
		public static IList<HDataTable> getDataTables(long funcId)
		{
			IList<HDataTable> obj = QHttpClient.PostList<HDataTable>(getBaseUrl("getDataTables"), new { funcId = funcId });
			return obj;
		}

		/// <summary>
		/// 获取pdm文件中所有的数据表
		/// </summary>
		/// <param name="projectId"></param>
		/// <returns></returns>
		public static IList<HDataTable> getPdmDataTableNames(long projectId)
		{
			IList<HDataTable> obj = QHttpClient.PostList<HDataTable>(getBaseUrl("getPdmDataTableNames"), new { projectId = projectId });
			return obj;
		}

		/// <summary>
		/// 获取项目中所有注册的数据表
		/// </summary>
		/// <param name="projectId"></param>
		/// <returns></returns>
		public static IList<HDataTable> getProjectDataTables(long projectId)
		{
			IList<HDataTable> obj = QHttpClient.Post<List<HDataTable>>(getBaseUrl("getProjectDataTables"), new { projectId = projectId });
			return obj;
		}

		/// <summary>
		/// 从pdm文件中读取表和字段
		/// </summary>
		/// <param name="projectId"></param>
		/// <returns></returns>
		public static HDataTable getTableWithColumnsByTableName(long projectId, string tableName)
		{
			HDataTable obj = QHttpClient.Post<HDataTable>(getBaseUrl("getTableWithColumnsByTableName"), new { projectId = projectId, tableName = tableName });
			return obj;
		}
		/// <summary>
		/// 从pdm文件中读取表字段
		/// </summary>
		/// <param name="projectId"></param>
		/// <returns></returns>
		public static List<HDataColumn> getDataColumnsByTableName(long projectId, string tableName)
		{
			List<HDataColumn> obj = QHttpClient.Post<List<HDataColumn>>(getBaseUrl("getDataColumnsByTableName"), new { projectId = projectId, tableName= tableName });
			return obj;
		}

		public static HDataTable getDataTable(long id)
		{
			HDataTable obj = QHttpClient.Post<HDataTable>(getBaseUrl("getDataTable"), new { id = id });
			return obj;
		}

		public static DataTableVO getDataTableVO(long id)
		{
			DataTableVO obj = QHttpClient.Post<DataTableVO>(getBaseUrl("getDataTableVO"), new { id = id });
			return obj;
		}

		public static void deleteDataTable(long id)
		{
			QHttpClient.Execute(getBaseUrl("deleteDataTable"), new { id = id });
		}

		public static HDataTable updateProp(long id, string propName, string value)
		{
			HDataTable obj = QHttpClient.Post<HDataTable>(getBaseUrl("updateProp"), new { id = id, propName = propName, value = value });
			return obj;
		}

		public static void syncColumns(long datatableId)
		{
			QHttpClient.Execute(getBaseUrl("syncColumns"), new { datatableId = datatableId });
		}
	}
}
